<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<!--
功能模块: ${codeName}
开发信息: 
-->
<sqlMap namespace="${className}Mapper">
	<typeAlias alias="${className}" type="${entityPackage}.${className}"/>
	
	<sql id="commonSelect">
		<#list columnDatas as item>
		   <#if item_index==0>
		      ${tablesAsName}.${item.columnName}	   AS	${item.domainPropertyName}
		   <#else>
			 ,${tablesAsName}.${item.columnName}	   AS	${item.domainPropertyName}
		   </#if>
		</#list>
	</sql>
	
	<sql id="whereCondition">
		<dynamic>
			<#list columnDatas as item>
			<#if item.dataType == "Date">
				 <isNotEmpty prepend="and" property="querySectionMap.${item.domainPropertyName}Begin">
	  		 		${tablesAsName}.${item.columnName} <![CDATA[>=]]> #querySectionMap.${item.domainPropertyName}Begin#
				 </isNotEmpty>
				 <isNotEmpty prepend="and" property="querySectionMap.${item.domainPropertyName}End">
	  		 		${tablesAsName}.${item.columnName} <![CDATA[<=]]> #querySectionMap.${item.domainPropertyName}End#
				 </isNotEmpty>
			<#elseif item.dataType == "BigDecimal">
				 <isNotEmpty prepend="and" property="querySectionMap.${item.domainPropertyName}Begin">
	  		 		${tablesAsName}.${item.columnName} <![CDATA[>=]]> #querySectionMap.${item.domainPropertyName}Begin#
				 </isNotEmpty>
				 <isNotEmpty prepend="and" property="querySectionMap.${item.domainPropertyName}End">
	  		 		${tablesAsName}.${item.columnName} <![CDATA[<=]]> #querySectionMap.${item.domainPropertyName}End#
				 </isNotEmpty>
			<#else>
				 <isNotEmpty prepend="and" property="${item.domainPropertyName}">
	  		 		${tablesAsName}.${item.columnName} = #${item.domainPropertyName}#
				 </isNotEmpty>
			</#if>
			</#list>
	  	</dynamic>
	</sql>
	
	<!--user customize code start-->
${userCustomCode}
	<!--user customize code end  -->
	
	<!--
	方法名称: insert
	调用路径: ${className}Mapper.insert
	开发信息: 
	处理信息: 保存记录
	-->
 	<insert id="insert" parameterClass="${className}" >
	 	INSERT  INTO  ${tableName}
					(
				<dynamic prepend=" ">
				 <#list columnDatas as item>
					<#if item_index==0>
					<isNotEmpty  property="${item.domainPropertyName}" prepend=",">${item.columnName}</isNotEmpty>
					<#else>
					<isNotEmpty  property="${item.domainPropertyName}" prepend=",">${item.columnName}</isNotEmpty>
					</#if>
				</#list>
				</dynamic>
				<![CDATA[ )values( ]]>
				<dynamic prepend=" ">
				 <#list columnDatas as item>
					<#if item_index==0>
					<isNotEmpty  property="${item.domainPropertyName}" prepend=",">#${item.domainPropertyName}#</isNotEmpty>
					<#else>
					<isNotEmpty  property="${item.domainPropertyName}" prepend=",">#${item.domainPropertyName}#</isNotEmpty>
					</#if>
				</#list>
				</dynamic>
				<![CDATA[ ) ]]>
				<#list columnKeyDatas as item>
				<#if item.autoIncrement>
				<selectKey resultClass="long" keyProperty="${item.domainPropertyName}">
        			SELECT @@IDENTITY AS ID
				</selectKey>
				</#if>
				</#list>
	</insert>
	
	
	<!--
	方法名称: update
	调用路径: ${className}Mapper.update
	开发信息: 
	处理信息: 修改记录
	-->
 	<update id="update" parameterClass="${className}" >
		UPDATE   ${tableName}
	  	  <dynamic prepend=" SET ">
				<#list columnDatas as item>
					<#if item.keyColumn>
					<#elseif item.columnName == 'MODIFIED_TIME'>
					<#else>
						<#if item.columnName == 'LAST_UPDATE_NO' >
					<isNotEmpty property="${item.domainPropertyName}" prepend=",">${item.columnName} = ${item.columnName} + 1</isNotEmpty>
						<#else>
					<isNotEmpty property="${item.domainPropertyName}" prepend=",">${item.columnName} = #${item.domainPropertyName}#</isNotEmpty>
					 	</#if>
					</#if>
				</#list>
	  	  </dynamic>
				WHERE
				<#list columnKeyDatas as item>
					<#if item_index==0>
	  	 		 		${item.columnName} = #${item.domainPropertyName}#
					<#else>
						 AND ${item.columnName} =#${item.domainPropertyName}#
					</#if>
				</#list>
				<#list columnDatas as item>
				<#if item.columnName == 'LAST_UPDATE_NO'>
					<isNotEmpty property="${item.domainPropertyName}" > AND ${item.columnName} = #${item.domainPropertyName}#</isNotEmpty>
			 	</#if>
		</#list>
	</update>

	
	<#if columnKeyParam !="">
	<!--
	方法名称: deleteByPriKey
	调用路径: ${className}Mapper.deleteByPriKey
	开发信息: 
	处理信息: 删除记录
	-->
	<delete id="deleteByPriKey" parameterClass="${className}">
		DELETE 	FROM ${tableName}
		WHERE 
		<#list columnKeyDatas as item>
			<#if item_index==0>
			${item.columnName} = #${item.domainPropertyName}#
			<#else>
		 AND ${item.columnName} = #${item.domainPropertyName}#
			</#if>
		</#list>
	</delete>
	</#if>
	
	<#if columnKeyParam !="">
	<!--
	方法名称: findByPriKey
	调用路径: ${className}Mapper.findByPriKey
	开发信息: 
	处理信息: 根据主键查询记录
	-->
	<select id="findByPriKey" parameterClass="${className}"  resultClass="${className}">
		   SELECT   
		   <include refid="commonSelect"/>
		   FROM   ${tableName}      AS ${tablesAsName}
		   WHERE
			<#list columnKeyDatas as item>
				<#if item_index==0>
				${item.columnName} = #${item.domainPropertyName}#
				<#else>
			 AND ${item.columnName} = #${item.domainPropertyName}#
				</#if>
		</#list>
	</select>
	</#if>
	
	<!--
	方法名称: getPagenationList
	调用路径: ${className}Mapper.getPagenationList
	开发信息: 
	处理信息: 分页查询记录
	-->
	<select id="getPagenationList" parameterClass="${className}"  resultClass="${className}">
		<!-- 分页条 -->
		<include refid="jr.commons.paginationPrefix"/>
		   SELECT
		   <include refid="commonSelect"/>
		   FROM   ${tableName}      AS ${tablesAsName}
		   WHERE 1=1
		 	<include refid="whereCondition"/>
		 	<isNotEmpty property="columnSort" prepend=" ORDER BY ">  $columnSort$ </isNotEmpty>
		 	<isEmpty property="columnSort" prepend=" ORDER BY ">  ${columnKeySort} </isEmpty>
		<!-- 分页条 -->
		<include refid="jr.commons.paginationSuffix"/>
	</select>
	
	<!--
	方法名称: getPagenationList-count
	调用路径: ${className}Mapper.getPagenationList-Count
	开发信息: 
	处理信息: 查询记录数
	-->
	<select id="getPagenationList-count" parameterClass="${className}" resultClass="java.util.HashMap">
		SELECT
			COUNT(1) CNT
		<#list columnDatas as item>
		   <#if item.needSum>
		    ,IFNULL(SUM(${tablesAsName}.${item.columnName}),0) AS	${item.columnName}_SUM
		   </#if>
		</#list>
		FROM  ${tableName}  AS ${tablesAsName}
		 WHERE 1=1
		<include refid="whereCondition"/>
	</select>
	
	<!--
	方法名称: getList
	调用路径: ${className}Mapper.getList
	开发信息: 
	处理信息: 查询所有的记录
	-->
	<select id="getList" parameterClass="${className}"  resultClass="${className}">
		   SELECT
		   <include refid="commonSelect"/>
		   FROM   ${tableName}      AS ${tablesAsName}
		   WHERE 1=1
		 	<include refid="whereCondition"/>
		 	<isNotEmpty property="columnSort" prepend=" ORDER BY ">  $columnSort$ </isNotEmpty>
		 	<isEmpty property="columnSort" prepend=" ORDER BY ">  ${columnKeySort} </isEmpty>
	</select>
	
</sqlMap>